package com.ruoyi.common.zq.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.common.zq.domin.ZqVolunteerActivity;
import com.ruoyi.common.zq.domin.dto.IndexDTO;
import com.ruoyi.common.zq.domin.vo.ActivityVO;
import com.ruoyi.common.zq.domin.vo.VolunteerActivityVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 志愿活动信息
 *
 * @Author: LoneyFeather
 * @Date: Created in 15:38 2021/12/24
 */
@Mapper
@Repository
public interface ZqVolunteerActivityMapper extends BaseMapper<ZqVolunteerActivity> {


    @Update("update zq_volunteer_activity " +
            "set apply_state = #{applyState} " +
            "where activity_id = #{activityId} " )
    void updateByApplyState(@Param("activityId") Long activityId,@Param("applyState") Integer applyState);


    @Select("<script>" +
            " select count(zva.apply_state) as num " +
            "from zq_volunteer_activity zva " +
            "where zva.activity_id in  " +
            "<foreach collection='activityIds' item='activity_id' open='(' separator=',' close=')'>" +
                " #{activity_id} " +
            "</foreach>" +
            "and zva.apply_state = 2 " +
            "</script>")
    Integer countApplyState(@Param("activityIds") Long[] activityIds);

    @Update("<script>" +
            "update zq_volunteer_activity zva " +
            "set zva.is_del = 1 " +
            "where zva.activity_id in " +
                "<foreach collection='activityIds' item='activity_id' open='(' separator=',' close=')'>" +
                " #{activity_id} " +
                "</foreach>" +
            "</script>")
    int updateIsDelByActivityIds(@Param("activityIds") Long[] activityIds);


    @Update("<script>" +
            " update zq_volunteer_activity " +
            " set " +
            " <if test=\"deptId != null\">dept_id = #{deptId},</if> " +
            " <if test=\"activityName != null and activityName != ''\"> activity_name = #{activityName}, </if>\n" +
            " <if test=\"intro != null and intro != ''\">\n" +
            " intro = #{intro},\n" +
            " </if>\n" +
            " <if test=\"activityTime != null and activityTime != ''\">\n" +
            " activity_time = #{activityTime},\n" +
            " </if>\n" +
            " <if test=\"volunteerTime != null\">\n" +
            " volunteer_time = #{volunteerTime},\n" +
            " </if>\n" +
            " <if test=\"place != null and place != ''\">\n" +
            " place = #{place},\n" +
            " </if>\n" +
            " <if test=\"num != null\">\n" +
            " num = #{num},\n" +
            " </if>\n" +
            " <if test=\"reward != null and reward != ''\">\n" +
            " reward = #{reward},\n" +
            " </if>\n" +
            " <if test=\"applyState != null\">\n" +
            " apply_state = #{applyState},\n" +
            " </if>\n" +
            " <if test=\"state != null\">\n" +
            " state = #{state},\n" +
            " </if>\n" +
            " <if test=\"beginTime != null\">\n" +
            " begin_time = #{beginTime},\n" +
            " </if>\n" +
            " <if test=\"endTime != null\">\n" +
            " end_time = #{endTime},\n" +
            " </if>\n" +
            " <if test=\"updateBy != null and updateBy != ''\">\n" +
            " update_by = #{updateBy},\n" +
            " </if>\n" +
            " <if test=\"updateTime != null\">\n" +
            " update_time = #{updateTime},\n" +
            " </if>\n" +
            " <if test=\"remark != null and remark != ''\">\n" +
            " remark = #{remark},\n" +
            " </if>\n" +
            "        where activity_id = #{activity.activityId} " +
            "</script>")
    public int updateVolunteerActivity(@Param("activity") ZqVolunteerActivity activity);





    @Select("<script>" +
            " SELECT zva.activity_id,zva.dept_id,sd.dept_name,zva.activity_name,zva.intro,zva.activity_time,zva.place,zva.num,zva.reward, " +
            "       zva.volunteer_time,zva.apply_state,sdd.dict_label as apply_state_name,zva.create_user_id,zva.state,zva.is_del,zva.begin_time,zva.end_time, " +
            "       zva.create_by,zva.create_time,zva.update_by,zva.update_time,zva.remark " +
            "FROM zq_volunteer_activity zva " +
            "left join sys_dept sd on zva.dept_id = sd.dept_id " +
            "left join sys_dict_data sdd on sdd.dict_value = zva.apply_state " +
            "right join sys_dict_type sdt on sdd.dict_type = sdt.dict_type " +
            "WHERE is_del = 0  " +
            "and sdt.dict_type = 'activity_apply_state' " +
            " <if test = 'activityDTO.deptIds != null and activityDTO.deptIds.length>0 ' > " +
            "and zva.dept_id in " +
            "  <foreach collection='activityDTO.deptIds' item='dept_id' open='(' separator=',' close=')'>" +
            "   #{dept_id} " +
            "  </foreach> " +
            " </if> " +
            " <if test = 'activityDTO.activityName != null' > AND zva.activity_name like concat('%',#{activityDTO.activityName},'%') </if>" +
            " <if test = 'activityDTO.applyState != null' > AND zva.apply_state = #{activityDTO.applyState} </if> " +
            " <if test = 'activityDTO.state != null' > AND zva.state = #{activityDTO.state} </if> " +
            "order by zva.create_time desc  " +
            "</script>")
    List<VolunteerActivityVO> selectListByVO(@Param("activityDTO") VolunteerActivityVO activityDTO);



    @Select("<script>" +
            "SELECT zva.activity_id,zva.dept_id,sd.dept_name,zva.activity_name,zva.intro,zva.activity_time,zva.place,zva.num, \n" +
            "       zva.reward, zva.volunteer_time,zva.apply_state,sdd.dict_label as apply_state_name,zva.create_user_id,zva.state,zva.is_del,zva.begin_time, \n" +
            "       zva.end_time, zva.create_by,zva.create_time,zva.update_by,zva.update_time,zva.remark \n" +
            "FROM zq_volunteer_activity zva \n" +
            "    left join sys_dept sd on zva.dept_id = sd.dept_id \n" +
            "left join sys_dict_data sdd on sdd.dict_value = zva.apply_state \n" +
            "right join sys_dict_type sdt on sdd.dict_type = sdt.dict_type \n" +
            "WHERE zva.is_del = 0 \n" +
            "  and sdt.dict_type = 'activity_apply_state' " +
            "  and zva.activity_id = #{activityId} " +
            "</script>")
    VolunteerActivityVO getByActivityId(@Param("activityId") Long activityId);


    /**
     * 首页查询活动列表
     * @param indexDTO
     */
    @Select("<script>" +
            "select zva.activity_id, zva.dept_id,sd.dept_name, zva.activity_name, zva.intro, zva.activity_time,\n" +
            "       zva.volunteer_time, zva.place, zva.num, zva.reward, zva.apply_state, sdd.dict_label as apply_state_name , zva.state,\n" +
            "       zva.begin_time, zva.end_time, zva.is_del, zva.create_user_id, zva.create_by,\n" +
            "       zva.update_by, zva.create_time, zva.update_time, zva.remark ,\n" +
            "       zeb.user_id, zeb.pitch_on, zeb.grade, zeb.comment\n" +
            "from zq_volunteer_activity zva\n" +
            "left join zq_entry_blank zeb on zva.activity_id = zeb.activity_id\n" +
            "left join sys_dept sd on zva.dept_id = sd.dept_id\n" +
            " left join sys_dict_data sdd on sdd.dict_value = zva.apply_state\n" +
            " right join sys_dict_type sdt on sdd.dict_type = sdt.dict_type " +
            "where zva.is_del = '0'\n" +
            " and sdt.dict_type = 'activity_apply_state' " +
            "  and zeb.user_id = #{indexDTO.userId} \n" +
            " <if test=\" indexDTO.activityName != null and indexDTO.activityName != '' \" >" +
                "and zva.activity_name like concat('%', #{indexDTO.activityName}, '%')\n" +
            "</if>" +
            " <if test=\" indexDTO.applyState != null and indexDTO.applyState != '' \" >" +
            "and zva.apply_state = #{indexDTO.applyState} " +
            "</if>" +
            " <if test=\" indexDTO.beginTime != null and indexDTO.beginTime != '' \" >" +
            "and date_format(zva.create_time,'%y%m%d') &gt;= date_format(#{indexDTO.beginTime},'%y%m%d')\n" +
            "</if>" +
            " <if test=\" indexDTO.endTime != null and indexDTO.endTime != '' \" >" +
            "and date_format(zva.create_time,'%y%m%d') &lt;= date_format(#{indexDTO.endTime},'%y%m%d')\n" +
            "</if>" +
            "order by zva.create_time desc" +
            "</script>")
    List<ActivityVO> listActivity(@Param("indexDTO") IndexDTO indexDTO);

    @Select("<script>" +
            "select zva.activity_id, zva.dept_id,sd.dept_name, zva.activity_name, zva.intro, zva.activity_time,\n" +
            "       zva.volunteer_time, zva.place, zva.num,zva.reward, zva.apply_state ,zva.apply_state,sdd.dict_label as apply_state_name , zva.state,\n" +
            "       zva.begin_time, zva.end_time, zva.is_del, zva.create_user_id,zva.create_by, zva.update_by,\n" +
            "       zva.create_time, zva.update_time, zva.remark " +
            "from zq_volunteer_activity zva " +
            "left join sys_dept sd on zva.dept_id = sd.dept_id " +
            " left join sys_dict_data sdd on sdd.dict_value = zva.apply_state\n" +
            " right join sys_dict_type sdt on sdd.dict_type = sdt.dict_type " +
            "where zva.is_del = 0 \n" +
            "and sdt.dict_type = 'activity_apply_state' " +
            "and zva.create_user_id = #{indexDTO.userId} " +
            " <if test=\" indexDTO.activityName != null and indexDTO.activityName != '' \" >" +
            "and zva.activity_name like concat('%', #{indexDTO.activityName}, '%')\n" +
            "</if>" +
            " <if test=\" indexDTO.applyState != null and indexDTO.applyState != '' \" >" +
            "and zva.apply_state = #{indexDTO.applyState} " +
            "</if>" +
            " <if test=\" indexDTO.beginTime != null and indexDTO.beginTime != '' \" >" +
            "and date_format(zva.create_time,'%y%m%d') &gt;= date_format(#{indexDTO.beginTime},'%y%m%d')\n" +
            "</if>" +
            " <if test=\" indexDTO.endTime != null and indexDTO.endTime != '' \" >" +
            "and date_format(zva.create_time,'%y%m%d') &lt;= date_format(#{indexDTO.endTime},'%y%m%d')\n" +
            "</if>" +
            "order by zva.create_time desc" +
            "</script>")
    List<ActivityVO> listActivityByadmin(@Param("indexDTO") IndexDTO indexDTO);
}
